package com.platform.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.annotation.IgnoreAuth;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.service.MallBranchService;
import com.platform.modules.mall.service.MallShopsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 地点云
 */
@Api(tags = "腾讯地图（地点云）")
@RequestMapping("/app/tencentMap")
@RestController
public class TencentMapsController {
    // todo 腾讯地图 hch old

    @Autowired
    MallShopsService shopsService;

    @Resource
    MallBranchService branchService;


    /**
     * 获取邻近店铺
     *
     * @param map location:经纬度，
     *            radius：范围（单位m）,
     *            mapType 1查询商家  2（或其他）查询网点
     *            page_index 当前页
     *            page_size 一页返回的数据
     * @return
     */
    @IgnoreAuth
    @GetMapping("getNearbyShops")
    @ApiOperation(value = "获取附近店铺", notes = "邻近 店铺 分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "当前页码", example = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "limit", value = "每页条数", example = "10", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "检索字段", example = "", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "longitude", value = "经度", example = "112.979933", dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "latitude", value = "纬度", example = "28.238647", dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "categoryId", value = "类别ID（注意I是大写）", dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "brandId", value = "品牌id", example = "", dataType = "String"),
    })
    public RestResponse getNearbyShops(@RequestParam Map<String, Object> map) {
        if (StringUtils.isBlank((String) map.get("longitude")) || StringUtils.isBlank((String) map.get("latitude"))) {
            return RestResponse.error("未获取到当前位置！");
        }
        map.putIfAbsent("range", 5);
        return RestResponse.success().put("data", shopsService.getNearbyShops(map));
    }


    /**
     * 获取邻近店铺
     *
     * @param map location:经纬度，
     *            radius：范围（单位m）,
     *            mapType 1查询商家  2（或其他）查询网点
     *            page_index 当前页
     *            page_size 一页返回的数据
     * @return
     */
    @IgnoreAuth
    @GetMapping("getNearbySpBc")
    @ApiOperation(value = "获取附近店铺和支行", notes = "邻近店铺和支行")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "当前页码", example = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "limit", value = "每页条数", example = "10", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "检索字段", example = "", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "location", value = "纬经度", example = "89.12356,112.979933", dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "1:店铺,2:支行,3:一起", dataType = "int")
    })
    public RestResponse getNearbySpBc(@RequestParam Map<String, Object> map) {
        if (StringUtils.isBlank((String) map.get("location"))) {
            return RestResponse.error("未获取到当前位置！");
        }
        map.putIfAbsent("range", 5);
        int type = Integer.parseInt(map.get("type").toString());
        IPage page = null;
        switch (type){
            case 1:
                page = shopsService.getNearbySimple(map);
                break;
            case 2:
                page = branchService.getNearbySimple(map);
                break;
            default:
                page = branchService.getNearbySimple(map);
                IPage page2 = shopsService.getNearbySimple(map);
                page.setTotal(Math.max(page2.getTotal(),page.getTotal()));
                if(page.getRecords() == null || page.getRecords().isEmpty()){
                    page.setRecords(page2.getRecords());
                }else if(page2.getRecords() != null && !page2.getRecords().isEmpty()){
                    page.getRecords().addAll(page2.getRecords());
                }
        }
        return RestResponse.success().put("data", page);
    }

}
